
package com.example.demo.modules.sys.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.result.PageResult;
import com.example.demo.modules.sys.model.SysMenu;
import com.example.demo.modules.sys.model.SysRole;
import com.example.demo.modules.sys.model.SysUser;
import com.example.demo.modules.sys.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.example.demo.core.entity.ProcessResult.ERROR;

/**
 * @author qjp
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @RequestMapping("/list")
    public ModelAndView list() {
        return new ModelAndView("/modules/sys/sysRole/list");
    }

    @RequestMapping
    public PageResult<SysRole> getAll(SysRole sysRole, String keyword, HttpServletRequest request) {
        List<SysRole> roleList = roleService.getAll(sysRole,keyword);
        return new PageResult(new PageInfo<SysRole>(roleList));
    }

    @PostMapping(value = "/batchDelete")
    public ProcessResult batchDelete(@RequestParam("ids[]") Integer[] ids) {
        try {
            roleService.batchDelete(ids);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/saveOrUpdate")
    public ProcessResult saveOrUpdate(SysRole sysRole) {
        try {
            roleService.saveOrUpdate(sysRole);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/view/{id}")
    public SysRole view(@PathVariable Integer id) {
        SysRole sysRole = roleService.getById(id);
        return sysRole;
    }

    @RequestMapping(value = "/delete/{id}")
    public ProcessResult delete(@PathVariable Integer id) {
        try {
            roleService.deleteById(id);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping("/form")
    public ModelAndView form() {
        return new ModelAndView("/modules/sys/sysRole/form");
    }

    @RequestMapping("/allotUserLayer")
    public ModelAndView allotUserLayer() {
        return new ModelAndView("/modules/sys/sysRole/allotUser");
    }

    @RequestMapping("/allotMenuLayer/{id}")
    public ModelAndView allotMenuLayer(@PathVariable Integer id, HttpServletRequest request) {
        request.getSession().setAttribute("roleId",id);
        return new ModelAndView("/modules/sys/sysRole/allotMenu");
    }
}
